Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for string type annotations in attrs #2

Merged
merged 4 commits into from
Feb 11, 2020
Merged

Conversation

ivanprado
Copy link
Contributor

It doesn't cover all the possible cases because there are limitations in attrs itself. See python-attrs/attrs#593

Enabled py35 in Travis

It doesn't cover all the possible cases because there are limitations in attrs itself. See python-attrs/attrs#593

Enabled py35 in Travis
@codecov
Copy link

codecov bot commented Feb 11, 2020

Codecov Report

Merging #2 into master will increase coverage by 2.12%.
The diff coverage is 100%.

@@            Coverage Diff             @@
##           master       #2      +/-   ##
==========================================
+ Coverage    92.1%   94.23%   +2.12%     
==========================================
  Files           3        3              
  Lines          38       52      +14     
  Branches        8        9       +1     
==========================================
+ Hits           35       49      +14     
  Misses          1        1              
  Partials        2        2
Impacted Files Coverage Δ
andi/andi.py 92.3% <100%> (+4.3%) ⬆️

@ivanprado ivanprado requested a review from kmike February 11, 2020 13:44
andi/andi.py Outdated
@@ -7,11 +9,40 @@
from andi.typeutils import get_union_args, is_union


def _get_globalns_as_get_type_hints(func) -> Dict:
""" Global namespace resolution extracted from ``get_type_hints`` method """
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this function copied from? It'd be nice to have a link here, maybe to cpython repo on github.

Could you please also check https://codecov.io/gh/scrapinghub/andi/compare/b64bf74d4182e3a96563c4e35ec10d396d2164a4...665c6a53bb5ba511dfbfcbe25223d915c91500e8/diff? It shows that parts of this code are never called in tests.

I'm asking because locally for me the relevant code is just

    if globalns is None:
        globalns = getattr(obj, '__globals__', {})

This is likely because it is from Python 3.6 - what Python version have you copied it from? It would be good to preserve this, as the code is changing upstream.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied from 3.7: https://github.com/python/cpython/blob/3.7/Lib/typing.py#L981-L988 . I've put the link in the docstring here:3cf30d3

It shows that parts of this code are never called in tests.

I'll try to create some tests that cover these lines. I'll have to figure out what is the __wrapped__ for.

andi/andi.py Outdated
@@ -7,11 +9,41 @@
from andi.typeutils import get_union_args, is_union


def _get_globalns_as_get_type_hints(func) -> Dict:
""" Global namespace resolution extracted from ``get_type_hints`` method.
Python 3.7 (https://github.com/python/cpython/blob/3.7/Lib/typing.py#L981-L988) """
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, it is different again in Python 3.8; in this case it mentions sys.modules[func.__module__].__dict__. Would you mind checking how it works in Python 3.8 - are workarounds still needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently this part of the code is the same in 3.7 and 3.8. I think it is right as it is.

ns.update(_get_globalns_as_get_type_hints(func))
return ns


def inspect(func: Callable) -> Dict[str, List[Optional[Type]]]:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kmike an alternative to fix the attrs problem would be to provide a inspect for classes. Something like:

def inspect_cls(cls: type):
    ...
   annotations = get_type_hints(cls.__init__, cls.__globals__)
    ...

In this case I think we would have access to the global namespace easily. What do you think?

@kmike
Copy link
Member

kmike commented Feb 11, 2020

Thanks @ivanprado! I think that's fine to merge it as-is.

@kmike kmike merged commit 963099f into master Feb 11, 2020
@ivanprado
Copy link
Contributor Author

Thanks @kmike for the review!

@kmike kmike deleted the travis_tox_py35 branch April 23, 2020 15:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants